

"""
3 3
1 2 4 8 6
2 0 5 1 7
3 4 0 0 8
4 5 0 3 1
5 0 0 4 2
6 7 1 9 0
7 0 2 6 0
8 1 3 0 9
9 6 8 0 0
"""


def firstInput():
    row, col = map(int, input().split())
    hashMap = {}
    for i in range(row * col):
        temp = list(map(int, input().split()))
        hashMap[temp[0]] = temp[1:]
    return row, col, hashMap


def main(row, col, hashMap):
    # row, col, hashMap = firstInput()
    n = row * col
    left, right, top, bottom = [], [], [], []
    leftTop, rightTop, leftBottom, rightBottom = 0, 0, 0, 0
    res = [[0 for _ in range(col)] for _ in range(row)]
    for i in range(1, n + 1):
        curList = hashMap[i]
        #  左 上 右  下
        #  左边是0 上边是0
        if curList[0] == 0 and curList[1] == 0:
            leftTop = i
            res[0][0] = i
        if curList[2] == 0 and curList[1] == 0:
            rightTop = i
            res[0][-1] = i
        if curList[0] == 0 and curList[3] == 0:
            leftBottom = i
            res[-1][0] = i
        if curList[2] == 0 and curList[3] == 0:
            rightBottom = i
            res[-1][-1] = i

    # print(leftTop, rightTop, leftBottom, rightBottom)
    # print(res)

    for i in range(row):
        for j in range(col):
            if res[i][j] != 0:
                curList = hashMap[res[i][j]]
                # print(curList)
                if curList[0] != 0:
                    res[i][j - 1] = curList[0]
                if curList[1] != 0:
                    res[i - 1][j] = curList[1]
                if curList[2] != 0:
                    res[i][j + 1] = curList[2]
                if curList[3] != 0:
                    res[i + 1][j] = curList[3]

    for i in range(row):
        for j in range(col):
            print(res[i][j], end=' ')
        print()

if __name__ == "__main__":
    row = 3
    col = 3
    hashMap = {1: [2, 4, 8, 6], 2: [0, 5, 1, 7], 3: [4, 0, 0, 8], 4: [5, 0, 3, 1], 5: [0, 0, 4, 2], 6: [7, 1, 9, 0],
               7: [0, 2, 6, 0], 8: [1, 3, 0, 9], 9: [6, 8, 0, 0]}
    main(row, col, hashMap)
